import Vue from 'vue';
import VueResource from 'vue-resource';

Vue.use(VueResource);

export default ({
  /*
  后台GET请求，统一处理系统错误
   */
  getData: function (path, param, callback) {
    param.mobile = Vue.prototype.$storage.getStorage('userData').mobile;
    if (!param.areacode) {
      param.areacode = Vue.prototype.$storage.getStorage('areacode');
    }
    const url = Vue.prototype.$global.PRO_API_URL + '/' + Vue.prototype.$global.APP_NAME;
    Vue.http.get(url + path, {params: param}).then((response) => {
      Vue.prototype.$utils.indicatorClose();
      var result = response.data;
      if (result.code === 1) {
        callback(result.data);
      } else {
        Vue.prototype.$utils.toastMessage(result.data);
      }
    }, (response) => {
      Vue.prototype.$utils.indicatorClose();
      if (response.status === 500) {
        Vue.prototype.$utils.toastMessage('没有获取到数据！');
      }
    });
  },
  /*
  后台POSTs请求，统一处理系统错误
   */
  postData: function (path, param, callback) {
    param.mobile = Vue.prototype.$storage.getStorage('userData').mobile;
    if (!param.areacode) {
      param.areacode = Vue.prototype.$storage.getStorage('areacode');
    }
    const url = Vue.prototype.$global.PRO_API_URL + '/' + Vue.prototype.$global.APP_NAME;
    Vue.http.post(url + path, param, callback).then((response) => {
      Vue.prototype.$utils.indicatorClose();
      var result = response.data;
      if (result.code === 1) {
        callback(result.data);
      } else {
        Vue.prototype.$utils.toastMessage(result.data);
      }
    }, (response) => {
      Vue.prototype.$utils.indicatorClose();
      if (response.status === 500) {
        Vue.prototype.$utils.toastMessage('没有获取到数据！');
      }
    });
  }
});
